package TreeNode;

import java.util.*;

public class Solution {
	public List<List<Integer>> levelorder(TreeNode root) {
		if(root == null) return new ArrayList<>();
		List<List<Integer>> res = new ArrayList<>();
		Queue<TreeNode> queue = new LinkedList<>();
		queue.add(root);int depth = 0;
		while(!queue.isEmpty()) {
			int len = queue.size();
			res.add(new ArrayList<>());
			for(int i = 0;i < len;i++) {
				TreeNode temp = queue.remove();
				res.get(depth).add(temp.val);
				if(temp.left != null) queue.add(temp.left);
				if(temp.right != null) queue.add(temp.right);
			}
			depth++;
		}
		return res;
	}
}
